jQuery(function() {

jQuery.noConflict()
//Get add Form
var html = 'MyTestForm';
var other = '"removeAction": "", "method":"", "description":""'; //Do not delete this is global
jQuery(document).bind('hidePopup', hidePopup);
jQuery('a#close').live('click',function(){
  jQuery(document).trigger('hidePopup');
});
function getFormEdite(uid,task, format, option, c, view, layout,form,row_id,method,row_item,other){
	
	//Set Default value of params
	(uid =='' || uid == 0 )?uid='':uid=uid;
	(task =='' || task == 0 )?task='getformedite':task=task;
	(format =='' || format == 0 )?format='raw':format=format;
	(option =='' || option == 0 )?option='com_wrecruit':option=option;
	(c =='' || c == 0 )?c='getform':c=c;
	(view =='' || view == 0 )?view='getform':view=view;
	(layout =='' || layout == 0 )?layout='CandidateEdite':layout=layout;
	(form =='' || form == 0 )?form='formwarning':form=form;
	(method =='' || method == 0 )?method='add':method=method;
	(row_item =='' || row_item == 0 )?row_item='':row_item=row_item;
	(other =='' || other == 0 )?other='':other=other;
	//End set Default value
	
	
	var html = jQuery.ajax({
		   url: 'index.php',
		   type: 'POST',
		   async:true,
		   data: {
				uid:uid,task:'getformedite',format:'raw',
				option:'com_wrecruit',c:'getform',
				view:'candidates',layout:'CandidateEdite',
				form:form,row_id:row_id,
				method:method,
				row_item:row_item,
				other:other
		   },
		   dataType: "html",
           async:false,
		   global: false,
		   success: function(msg){
			  
		   }
	}).responseText;
	
	return html;
}
//End Get add Form
function returnAjax(data){
	html = data;
}

//Add route
function getFormName(me){
	var id = jQuery(me).attr('ID');
	htmlId = id;
	var form = "";
	switch(id){
		
		//Candidate
		case "email":
			  form = 'emailform';
		      break;
		case "password":
			  form = 'passwordform';
		      break;
		case "skill":
		 	  form = 'skillform';
		      break;
	    case "packageform":
			  form = 'packageform';
			  break;
		case "post-cv-leading-page":
			  form = "post_cv_leading_page";
			  break;
		case "educationform":
			  form = "educationform";
			  break;
	    case "pro_experience":
			  form = "proexperienceform";
			  break;
		case "skill_form":
			  form = "skillform";
			  break;
		case "reference":
			 form = "reference";
			  break;
		case "undifiend":
			  form = 'undifiedform';
		      break;
	   //End candidate
	   
		default: form = "formwarning";
		
		
	}
	return form;
}

//Route edit action
function getFormNameEdite(me){
	var id = jQuery(me).attr('ID');
	htmlId = id;
	id = splitStrId(htmlId);// formart ID
	var form = ""; 
	switch(id){
	    //Edid CV
		case "cv_edit_byid":
			  form = 'edit_post_cv_leading_page';
		      break;
		//End edit cv
		case "cv_edit_pro":
			  form = 'editproexperienceform';
		      break;
		case "cv_edit_reference":
			  form = 'editreference';
		      break;
        case "cv_edit_skill":
			  form = 'editskillform';
		      break;
	    case "cv_edit_edu":
			  form = 'editeducation';
		      break;
			  
		default: form = "formwarning";
	}
	return form;
}
function splitStrId(strId){
	var substr = strId.split('_');
		str = substr[0]+'_'+substr[1]+'_'+substr[2];
		return str;
}
function jsonOneLevelBuilder(str){
	return "{"+str+"}";
}
function concatStrbase(str1,str2){
	return str1+","+str2;
}
//End route edite action
function getFormNameRemove(me){
	var id = jQuery(me).attr('ID');
	htmlId = id;
	id = splitStrId(htmlId);// formart ID
	var form = "";
	
	switch(id){
	
		/* Remove cv */
		case "cv_remove_byid":
		     other = '"removeAction": "cv_upload_copy_paste", "method":"delete", "description":"cv"';
		     form = 'removeCvForm';
		     break;
			 
	    /* Remove Item CV */
		case "cv_remove_pro":
			  other = '"removeAction":"addprofessionalexperience", "method":"delete", "description":"professional"';
			  form = 'removeCvForm';
		      break;
		case "cv_remove_reference":
			  other = '"removeAction": "addReferenceCv", "method":"delete", "description":"reference"';
			  form = 'removeCvForm';
		      break;
        case "cv_remove_skill":
			  other = '"removeAction":"addSkills", "method":"delete", "description":"skill"';
			  form = 'removeCvForm';
		      break;
	    case "cv_remove_edu":
			  other = '"removeAction": "addEduCv", "method":"delete", "description":"education"';
			  form = 'removeCvForm';
		      break;
		
		  
		default: form = "formwarning";
	}
	return form;
}
//Remove form


//End remove form 
var popup = false;
jQuery(".wpopup").mouseover(
           function()
           { 
			// jQuery(this).css('text-decoration','underline');
           }  
);
jQuery(".wpopup").mouseout(
           function()
           {  
			 jQuery(this).css('text-decoration','none');
           }  
);
//Get add popup
jQuery(".wpopup").click(function(e){
	e.preventDefault();
	transformLoadingBg(jQuery(this));
	var uid = jQuery('#uid').val();
	var row_id = jQuery('#row_id').val();
	var formType = getFormName(jQuery(this));
	jQuery('#popupContainer').html(getFormEdite(uid,'', '', '', '', '', '',formType,row_id,'','',''));		
	jQuery('#popupContainer').find('select');
	if(popup == false){
		jQuery("#overlayEffect").css({
			"opacity": "0.7"
		});
		jQuery("#overlayEffect").fadeIn(100);
		jQuery("#popupContainer").fadeIn(100);
		jQuery("#close").fadeIn(100);
	    center();
		popup = true;
	}
	transformBackToDefaultBg(jQuery(this));
});


//Get edit popup	
jQuery(".cv_item_edite").click(function(e){
	e.preventDefault();
	var uid = jQuery('#uid').val();
	var row_id = jQuery('#row_id').val();
	var formType = getFormNameEdite(jQuery(this));
	var method = 'edit';
	var substrID = jQuery(this).attr('id').split('_');
	var row_item = '';
		if(substrID.length>=4){
			row_item = substrID[3];
	    }
	
	jQuery('#popupContainer').html(getFormEdite(uid,'', '', '', '', '', '',formType,row_id,method,row_item,''));		
	jQuery('#popupContainer').find('select');
	if(popup == false){
		jQuery("#overlayEffect").fadeIn(100);
		jQuery("#popupContainer").fadeIn(100);
		jQuery("#close").fadeIn(100);
	    center();
		popup = true;
	}	
});


//Remove 
jQuery(".cv_item_remove").click(function(e){
	e.preventDefault();
	transformLoading(jQuery(this)); //transform
	var uid = jQuery('#uid').val();
	var row_id = jQuery('#row_id').val();
	var formType = getFormNameRemove(jQuery(this));
	var method = 'remove';
	var substrID = jQuery(this).attr('id').split('_');
	var row_item = '';
		if(substrID.length>=4){
			row_item = substrID[3];
	    }
	jQuery('#popupContainer').html(getFormEdite(uid,'', '', '', '', '', '',formType,row_id,method,row_item,jsonOneLevelBuilder(other)));	
	jQuery('#popupContainer').find('select');
	if(popup == false){
		jQuery("#overlayEffect").fadeIn(100);
		jQuery("#popupContainer").fadeIn(100);
		jQuery("#close").fadeIn(100);
	    center();
		popup = true;
	}	
   transformBackToDefault(jQuery(this));
});

//Validator
	
//End validator



var transform = '';
//Img
function transformLoading(me){
	var id = jQuery(me).attr('ID');
	transform = jQuery('#'+id).html();
	    jQuery('#'+id).html("<img alt='' src='http://"+window.location.hostname+"/components/com_wrecruit/images/loading.gif'  width='20' hieght='20'/>");
	
}
function transformBackToDefault(me){
	var id = jQuery(me).attr('ID');
	    jQuery('#'+id).html(transform);
}
//For bg img
var btn_value ='';
function transformLoadingBg(me){
	var id = jQuery(me).attr('ID');
	btn_value = jQuery('#'+id).val();
	    jQuery('#'+id).val('loading');
	
}

function transformBackToDefaultBg(me){
	var id = jQuery(me).attr('ID');
	    jQuery('#'+id).val(btn_value);
}
	
	
jQuery("a#close").click(function(){
	hidePopup();
});
/*jQuery("#popupContainer").click(function(){
	hidePopup();
});*/

jQuery("#overlayEffect").click(function(){
	hidePopup();
});





	
function center(){
	/*var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = jQuery("#popupContainer").height();
	var popupWidth = jQuery("#popupContainer").width();
	jQuery("#popupContainer").css({
		"position": "absolute",
		"top": windowHeight,
		"left": windowWidth/2-popupWidth/2
	});*/
	jQuery('#popupContainer').each(function(){
		jQuery(this).css('left',(jQuery(window).width()-jQuery(this).outerWidth())/ 2 + 'px');
		jQuery(this).css('top',(jQuery(window).height()-jQuery(this).outerHeight())/ 2 + 'px');
	});
}
/*function ListenerHidePopup(){
	jQuery("#close").click(function(){
		hidePopup();
	});
}*/
function hidePopup(){
	if(popup==true){
		jQuery("#overlayEffect").fadeOut(100);
		jQuery("#popupContainer").fadeOut(100);
		jQuery("#close").fadeOut(100);
		popup = false;
	}
}

} ,jQuery);


